package com.zer0.tga.entity.livingentity.mob;

import com.zer0.tga.art.ArtArchive;
import com.zer0.tga.art.Bitmap;
import com.zer0.tga.entity.Entity;
import com.zer0.tga.entity.livingentity.Teemo;
import com.zer0.tga.entity.livingentity.flyingentity.EnemyBullet;
import com.zer0.tga.entity.livingentity.flyingentity.TeemoDart;
import com.zer0.tga.level.Room;
import com.zer0.tga.math.Coordinate;
import com.zer0.tga.math.Direction;
import com.zer0.tga.math.RandNum;
import com.zer0.tga.tile.GrassTile;
import com.zer0.tga.tile.Tile;

public class RedWraith extends Mob{
	
	private int actionTime = 250;
	private int teleportTime = 375; 
	private int actionCooldown = 250;
	
	private Coordinate playerPos;
	
	public void init()
	{
		//System.out.println("init");
		unitType = UnitType.ENEMY;
		health = maxHealth = 10;
		movementSpeed = 2.0;
	}

	@Override
	public void action() 
	{
		for(Entity e:level.getCurrentRoom().entities)
		{
			if (e instanceof Teemo)
			{
				playerPos = new Coordinate(e.position.x,e.position.y);
				break;
			}		    	
		}
		
		double distX, distY;
		Direction dirX,dirY;
		
		if(position.x > playerPos.x)
		{
			dirX = Direction.WEST;
			distX = position.x - playerPos.x;
		}
		else
		{
			dirX = Direction.EAST;
			distX = playerPos.x - position.x;
		}
		
		if(position.y > playerPos.y)
		{
			dirY = Direction.NORTH;
			distY = position.y - playerPos.y;
		}
		else
		{
			dirY = Direction.SOUTH;
			distY = playerPos.y - position.y;
		}
		
		if(distX>distY)
			direction = dirX;
		else
			direction = dirY;
		
		
		
		if(livingTime == actionTime)
		{

			level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x  ,position.y+1),level));
			level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x  ,position.y-1),level));
			level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x+1,position.y  ),level));
			level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x+1,position.y+1),level));
			level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x+1,position.y-1),level));
			level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x-1,position.y  ),level));
			level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x-1,position.y+1),level));
			level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x-1,position.y-1),level));
			actionTime += actionCooldown;
		}
		
		if(livingTime == teleportTime)
		{
			
			Coordinate newPos = new Coordinate();
			
			do
			{
				newPos.x = RandNum.getRandNum(1,Room.ROOM_X-1);
				newPos.y = RandNum.getRandNum(1,Room.ROOM_Y-1);
			}
			while(!(level.getCurrentRoom().RoomInterior[newPos.getX() + newPos.getY() * Room.ROOM_X] instanceof GrassTile));
		
			newPos.x *= Tile.WIDTH;
			newPos.y *= Tile.HEIGHT;
			
			newPos.x += Tile.WIDTH/2;
			newPos.y += Tile.HEIGHT/2;
			
			position = newPos;
			
			teleportTime += actionCooldown;
		}
	}
	
	@Override
	public Bitmap getArt() {
		// TODO Auto-generated method stub
				
		return ArtArchive.badguy[0][direction.getDirection()-1];
	}
	

	public boolean collision(Entity entity, Direction dir) 
    {
		if(entity instanceof TeemoDart)
		{
			hurt(((TeemoDart) entity).damage);
			entity.removed = true;
			return true;
		}

		return true;
    }


}